package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @version 1.0
 * @Author 杨旭康
 * @Date 2022/11/6 14:38
 * @question:
 */


@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加商品进购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> addR(@RequestBody ShoppingCart shoppingCart) {
        //userId number creatTime为空
        log.info("购物车数据: {}", shoppingCart);

        //获取用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //创建查询条件
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);

        //查询完用户id之后 还得判断该用户 下单的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            //买的是菜品
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //买的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //根据条件可以查询出买的是什么
        ShoppingCart cartServiceOne = shoppingCartService.getOne(wrapper);

        //调用service查询 ==> 此处查询不会有数据 (最开始查到此处 cartServiceOne都为null 走else逻辑 添加之后为1)
        if (cartServiceOne != null) {
            //不是第一次 直接调用修改方法
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //是第一次 调用添加方法 使用shoppingCart设置 完事赋值给cartServiceOne
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            //因为现在cartServiceOne值为null,所以需要将此值赋给他
            cartServiceOne = shoppingCart;
        }

        //前端需要返回去数据
        return R.success(cartServiceOne);
    }

    /**
     * 显示购物车数据
     * 请求 URL: http://localhost:8080/shoppingCart/list
     * 请求方法: GET
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> listR() {

        log.info("显示购物车数据....");

        //获取用户id
        Long currentId = BaseContext.getCurrentId();

        //条件判断
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);
        wrapper.orderByAsc(ShoppingCart::getCreateTime);

        //调用service
        List<ShoppingCart> list = shoppingCartService.list(wrapper);

        return R.success(list);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        //SQL:delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    /**
     * 如果多买了积分 商品 想要点击 - 去调几份时
     * 思路:判断如果该商品还剩一份时,执行删除操作;如果大于一份执行修改操作
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> subR(@RequestBody ShoppingCart shoppingCart) {
        //自动封装上了 菜品/套餐id
        log.info("减少商品: {}", shoppingCart);

        //同添加逻辑类似
        //获取用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //创建查询条件
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);

        Long dishId = shoppingCart.getDishId();

        if (dishId != null) {
            //修改的是菜品
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //修改的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //进行 菜品/套餐 的查询
        ShoppingCart cartServiceOne = shoppingCartService.getOne(wrapper);

        //对数量进行判断
        if (cartServiceOne.getNumber() > 1) {
            //执行修改操作
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number - 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //执行删除操作
            shoppingCartService.removeById(cartServiceOne);
        }

        return R.success(cartServiceOne);
    }


}
